package com.example.pt.service;

import com.example.pt.entity.TorrentFile;
import com.example.pt.entity.TorrentFileES;
import com.example.pt.entity.User;
import com.example.pt.entity.UserES;
import com.example.pt.repository.UserESRepository;
import com.example.pt.repository.UserRepositories;
import com.example.pt.mapper.UserMapper;
import com.example.pt.utils.UserToUserESUtils;
import jakarta.transaction.Transactional;
import org.modelmapper.ModelMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.sql.Date;
import java.time.LocalDate;
import java.util.List;

@Service
public class UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserService.class);

    private UserRepositories userRepositories;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserToUserESUtils userToUserESUtils;

    public UserService(UserRepositories userRepositories) {
        this.userRepositories = userRepositories;
    }

    public User searchByUid(int uid){
        User user = userRepositories.findByUid(uid);
        return user;
    }


    /**
     * 根据用户名搜索用户
     * @param username 用户名关键字
     * @return 用户列表
     */
    public List<User> searchByUsername(String username) {
        return userMapper.searchUsersByUsername(username);
    }

    public boolean creatNewUser(int uid, String username, String password, String email, int usertype, String headShot,
                                int level, int points){
        User user = new User();
        user.setUid(uid);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setUsertype(usertype);
        user.setHeadshot(headShot);
        user.setLevel(level);
        user.setPoints(points);
        user.setStatus(0);
        userRepositories.save(user);
        return true;
    }

    public boolean creatNewUser(int uid, String username, String password, String email, int usertype, String headShot,
                                int level, int points, Date register_day){
        User user = new User();
        user.setUid(uid);
        user.setUsername(username);
        user.setPassword(password);
        user.setEmail(email);
        user.setUsertype(usertype);
        user.setHeadshot(headShot);
        user.setLevel(level);
        user.setPoints(points);
        user.setStatus(0);
        user.setRegisterDate(register_day);
        userRepositories.save(user);

        userToUserESUtils.syncToES(user);

        return true;
    }

    //封禁用户
    @Transactional // 声明事务（更新操作必须添加）
    public boolean banUsers(Integer uid) {
        int rows = userRepositories.updateStatusByUid(uid, 1);

        if(rows > 0){
            User user = userRepositories.findByUid(uid);
            user.setStatus(1);
            userToUserESUtils.syncToES(user);
        }

        return rows > 0; // 返回是否更新成功
    }

    public boolean changeBufferDay(int uid) {
        User user = searchByUid(uid);
        user.setBufferDay(null);
        return true;
    }

    //用户危险状态2 并且更新违规时间
    public boolean status2AndBufferDayUser(User user) {
        user.setStatus(2);
        user.setBufferDay(Date.valueOf(LocalDate.now()));
        if(user.getStatus() == 2 && user.getBufferDay() != null){
            userToUserESUtils.syncToES(user);
            return true;
        }else{
            logger.info("用户{}" + user.getUid() + "处于危险状态2失败，请管理员进行检查！");
            return false;
        }
    }

    public boolean setLastLoginTime(int uid, Date date) {
        User user = searchByUid(uid);
        user.setLastTime(date);
       // System.out.println(date);
        userToUserESUtils.syncToES(user);
        userRepositories.save(user);
        return true;
    }


}
